package club.bigtian.statusbar;

import com.intellij.icons.AllIcons;
import com.intellij.ui.JBColor;
import com.intellij.util.ui.JBUI;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 账户信息卡片组件
 * 参考设计：深色主题，圆角卡片，包含域名、邮箱、过期时间、剩余额度等信息
 */
public class AccountCard extends JPanel {
    
    // 使用JBColor自动适配亮色/暗色主题
    private static final Color CARD_BORDER = JBColor.border();  // 使用默认边框颜色
    private static final Color EXPIRE_BG = new JBColor(new Color(217, 119, 6), new Color(217, 119, 6));  // 过期时间背景（橙黄色）
    private static final Color BALANCE_COLOR = new JBColor(new Color(34, 197, 94), new Color(34, 197, 94));  // 剩余额度颜色（绿色）
    private static final Color STATUS_BG = new JBColor(new Color(20, 184, 166), new Color(20, 184, 166));  // 状态标签背景（青色）
    
    private String domain;
    private String email;
    private Date expireDate;
    private int balance;
    private String status;
    
    public AccountCard(String domain, String email, Date expireDate, int balance, String status) {
        this.domain = domain;
        this.email = email;
        this.expireDate = expireDate;
        this.balance = balance;
        this.status = status;
        
        initUI();
    }
    
    private void initUI() {
        setLayout(new BorderLayout(8, 8));
        // 使用默认背景色
        setBackground(JBColor.background());
        setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(CARD_BORDER, 1, true),
            new EmptyBorder(12, 15, 12, 15)
        ));

        // 设置圆角效果
        setOpaque(true);

        // 顶部：域名和状态
        JPanel topPanel = createTopPanel();
        add(topPanel, BorderLayout.NORTH);

        // 中间：详细信息
        JPanel centerPanel = createCenterPanel();
        add(centerPanel, BorderLayout.CENTER);

        // 底部：操作按钮
        JPanel bottomPanel = createBottomPanel();
        add(bottomPanel, BorderLayout.SOUTH);

        // 设置更紧凑的尺寸
        setPreferredSize(new Dimension(360, 140));
        setMaximumSize(new Dimension(380, 150));
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制圆角背景 - 使用默认背景色
        g2.setColor(getBackground());
        g2.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);

        // 绘制圆角边框
        g2.setColor(CARD_BORDER);
        g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);

        g2.dispose();
    }
    
    /**
     * 创建顶部面板（域名 + 状态标签）
     */
    private JPanel createTopPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setOpaque(false);

        // 域名标题 - 使用默认前景色
        JLabel domainLabel = new JLabel(domain);
        domainLabel.setFont(new Font("Arial", Font.BOLD, 15));
        panel.add(domainLabel, BorderLayout.WEST);

        // 状态标签
        JLabel statusLabel = new JLabel(status);
        statusLabel.setFont(new Font("Arial", Font.PLAIN, 10));
        statusLabel.setForeground(Color.WHITE);
        statusLabel.setOpaque(true);
        statusLabel.setBackground(STATUS_BG);
        statusLabel.setBorder(new EmptyBorder(2, 8, 2, 8));
        statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(statusLabel, BorderLayout.EAST);

        return panel;
    }
    
    /**
     * 创建中间面板（时间、邮箱、过期、余额）- 更紧凑的布局
     */
    private JPanel createCenterPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setOpaque(false);
        panel.setBorder(new EmptyBorder(6, 0, 6, 0));

        // 当前时间 - 使用灰色
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        JLabel timeLabel = new JLabel(sdf.format(new Date()));
        timeLabel.setFont(new Font("Arial", Font.PLAIN, 11));
        timeLabel.setForeground(JBColor.GRAY);
        panel.add(timeLabel);

        panel.add(Box.createVerticalStrut(5));

        // 邮箱信息 - 更紧凑
        JPanel emailPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 3, 0));
        emailPanel.setOpaque(false);

        JLabel emailIcon = new JLabel(AllIcons.General.User);
        emailPanel.add(emailIcon);

        JLabel emailLabel = new JLabel(email);
        emailLabel.setFont(new Font("Monospace", Font.PLAIN, 11));
        emailLabel.setForeground(new Color(96, 165, 250));
        emailPanel.add(emailLabel);

        JButton copyBtn = createIconButton(AllIcons.Actions.Copy, "复制邮箱");
        emailPanel.add(copyBtn);

        panel.add(emailPanel);

        panel.add(Box.createVerticalStrut(6));

        // 过期时间和余额 - 在同一行
        JPanel infoPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 6, 0));
        infoPanel.setOpaque(false);

        // 过期时间标签 - 更小
        SimpleDateFormat shortSdf = new SimpleDateFormat("MM/dd HH:mm");
        JLabel expireLabel = new JLabel("过期: " + shortSdf.format(expireDate));
        expireLabel.setFont(new Font("Arial", Font.BOLD, 10));
        expireLabel.setForeground(Color.WHITE);
        expireLabel.setOpaque(true);
        expireLabel.setBackground(EXPIRE_BG);
        expireLabel.setBorder(new EmptyBorder(2, 6, 2, 6));
        infoPanel.add(expireLabel);

        // 余额标签 - 更小
        JLabel balanceLabel = new JLabel("剩余: " + balance);
        balanceLabel.setFont(new Font("Arial", Font.BOLD, 10));
        balanceLabel.setForeground(BALANCE_COLOR);
        infoPanel.add(balanceLabel);

        // 图表按钮
        JButton chartBtn = createIconButton(AllIcons.Actions.Show, "查看统计");
        infoPanel.add(chartBtn);

        panel.add(infoPanel);

        return panel;
    }
    
    /**
     * 创建底部操作按钮面板 - 更紧凑
     */
    private JPanel createBottomPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        panel.setOpaque(false);
        panel.setBorder(new EmptyBorder(6, 0, 0, 0));

        // 添加各种操作按钮 - 更小的尺寸
        panel.add(createActionButton(AllIcons.Actions.Execute, "打开"));
        panel.add(createActionButton(AllIcons.Actions.Share, "分享"));
        panel.add(createActionButton(AllIcons.Actions.Copy, "复制"));
        panel.add(createActionButton(AllIcons.Actions.Refresh, "刷新"));
        panel.add(createActionButton(AllIcons.Actions.Edit, "编辑"));
        panel.add(createActionButton(AllIcons.Actions.GC, "删除"));

        return panel;
    }
    
    /**
     * 创建操作按钮 - 更小更紧凑
     */
    private JButton createActionButton(Icon icon, String tooltip) {
        JButton button = new JButton(icon);
        button.setToolTipText(tooltip);
        button.setPreferredSize(new Dimension(32, 32));
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        // 鼠标悬停效果
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                button.setContentAreaFilled(true);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                button.setContentAreaFilled(false);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("点击了: " + tooltip);
            }
        });

        return button;
    }
    
    /**
     * 创建小图标按钮
     */
    private JButton createIconButton(Icon icon, String tooltip) {
        JButton button = new JButton(icon);
        button.setToolTipText(tooltip);
        button.setPreferredSize(new Dimension(24, 24));
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        
        return button;
    }
}

